package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StArtTableBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StArtTableVo;
import com.ruoyi.ruoyiyinzheng.domain.StArtTable;
import com.ruoyi.ruoyiyinzheng.mapper.StArtTableMapper;
import com.ruoyi.ruoyiyinzheng.service.IStArtTableService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工艺Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
public class StArtTableServiceImpl implements IStArtTableService {

    private final StArtTableMapper baseMapper;

    /**
     * 查询工艺
     */
    @Override
    public StArtTableVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工艺列表
     */
    @Override
    public TableDataInfo<StArtTableVo> queryPageList(StArtTableBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StArtTable> lqw = buildQueryWrapper(bo);
        Page<StArtTableVo> result = baseMapper.selectStArtTablePageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工艺列表
     */
    @Override
    public List<StArtTableVo> queryList(StArtTableBo bo) {
        LambdaQueryWrapper<StArtTable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStArtTableList(lqw);
    }

    private LambdaQueryWrapper<StArtTable> buildQueryWrapper(StArtTableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StArtTable> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), StArtTable::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StArtTable::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getArtVersion()), StArtTable::getArtVersion, bo.getArtVersion());
        lqw.eq(bo.getDocumentId() != null, StArtTable::getDocumentId, bo.getDocumentId());
        lqw.eq(bo.getGuidBookId() != null, StArtTable::getGuidBookId, bo.getGuidBookId());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), StArtTable::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增工艺
     */
    @Override
    public Boolean insertByBo(StArtTableBo bo) {
        StArtTable add = BeanUtil.toBean(bo, StArtTable.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工艺
     */
    @Override
    public Boolean updateByBo(StArtTableBo bo) {
        StArtTable update = BeanUtil.toBean(bo, StArtTable.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StArtTable entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工艺
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
